Basic

Output

러스트에서 출력은 일반적으로 println!을 사용합니다. 여기서 느낌표는 println!이 매크로임을 의미합니다. 따라서 println! 안의 내용에 따라 컴파일 단계에서 전처리 때 해당 부분이 다른 소스 코드로 대체됩니다.
fn main() {
    println!("Hello,");
    println!("dckim!");
}
Hello,
dckim!
만약 줄바꿈을 원치 않는다면 print! 매크로를 사용하면 됩니다.
fn main() {
    print!("Hello,");
    print!("dckim!");
}
Hello,dckim!
중괄호를 이용하여 변수를 출력할 수 있습니다.
fn main() {
    let a = 13;
    let b = 22;
    println!("a is {} and b is {}!", a, b);
}
a is 13 and b is 22!
중괄호 안에 $0$부터 시작하는 인덱스를 넣어 변수의 순서를 바꿀 수 있습니다.
fn main() {
    let a = 13;
    let b = 22;
    println!("a is {1} and b is {0}!", a, b);
}
a is 22 and b is 13!
중괄호 안에 파라미터로 넣을 변수를 지정해줄 수 있습니다.
fn main() {
    let a = 13;
    let b = 22;
    println!("a is {first} and b is {second}!", first=b, second=a);
}
a is 22 and b is 13!
만약 중괄호를 출력하고 싶다면 중괄호를 두 번 쓰면 됩니다.
fn main() {
    println!("{{ and }}");
}
{ and }
벡터나 여러 가지 구조체의 경우 단순히 중괄호를 사용하여 출력할 수 없습니다.
fn main() {
    let a = vec![1, 2, 3];
    println!("{}", a);
}
error[E0277]: `Vec<{integer}>` doesn't implement `std::fmt::Display`
 --> src\main.rs:3:20
  |
3 |     println!("{}", a);
  |                    ^ `Vec<{integer}>` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `Vec<{integer}>`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `prime` (bin "prime") due to previous error
대신 디버그 출력을 지원하는 경우 컴파일 에러 메시지에서 알려주는 것과 같이 {:?}를 넣으면 됩니다.
fn main() {
    let a = vec![1, 2, 3];
    println!("{:?}", a);
}
[1, 2, 3]